<!-- 实际呈现效果的组件 -->
<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref">seller display</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      myChart: null,
      allData: null,
      currentPage: 0,
      totalPage: 0,
      pageSize: 5,
      timerId: null,
    };
  },
  created() {
    this.$socket.registerCallBack("stockData", this.getData);
  },

  mounted() {
    this.initChart();
    this.getData();

    //改为websocket方式
    // this.$socket.send({
    //   action: "getData",
    //   socketType: "stockData",
    //   chartName: "stock",
    //   value: "",
    // });

    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
    this.startInterval();
  },
  destroyed() {
    this.$socket.unregisterCallBack("stockData");
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId);
  },

  methods: {
    //初始化echarts实例对象,要在dom结果加载完成后调用
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.stock_ref, "chalk");
      const initOption = {
        title: {
          text: "TOP零件库存分析",
          top: "10%",
          left: "20%",
        },
        series: [
          {
            type: "pie",
          },
          {
            type: "pie",
          },
          {
            type: "pie",
          },
          {
            type: "pie",
          },
          {
            type: "pie",
          },
        ],
      };
      this.myChart.setOption(initOption);

      this.myChart.on("mouseover", () => {
        // alert("mouseover");
        clearInterval(this.timerId);
      });
      this.myChart.on("mouseout", () => {
        // alert("mouseout");
        this.startInterval();
      });
    },
    // 获取数据
    async getData() {
      // await this.$http.get()
      const { data: ret } = await this.$http.get("stock");
      // console.log(retData);
      // 对allData进行赋值
      this.allData = ret;
      this.updateChart();
    },

    // 更新图表
    updateChart() {
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ];
      const start = this.currentPage * 5;
      const end = (this.currentPage + 1) * 5;
      const showData = this.allData.slice(start, end);
      const seriesArr = showData.map((item, index) => {
        return {
          type: "pie",
          name: item.name,
          radius: [110, 80],
          center: centerArr[index],
          label: {
            position: "center",
            fontSize: 20,
          },
          data: [
            {
              name: item.name + "\n" + item.sales,
              value: item.sales,
            },
            {
              // name: item.name + "\n" + item.stock,
              value: item.stock,
              itemStyle: {
                color: "grey",
              },
            },
          ],
        };
      });
      const dataOption = {
        series: seriesArr,
      };
      this.myChart.setOption(dataOption);

      this.screenAdapter();
    },

    screenAdapter() {
      // console.log(this.$refs.seller_ref.offsetWidth);
      const titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2.5;
      const outerRadius = innerRadius * 1.2;
      const labelFontSize = titleFontSize * 1.2;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: labelFontSize,
          },
        },
        series: [
          {
            label: {
              fontSize: labelFontSize,
            },
            radius: [outerRadius, innerRadius],
          },
          {
            label: {
              fontSize: labelFontSize,
            },
            radius: [outerRadius, innerRadius],
          },
          {
            label: {
              fontSize: labelFontSize,
            },
            radius: [outerRadius, innerRadius],
          },
          {
            label: {
              fontSize: labelFontSize,
            },
            radius: [outerRadius, innerRadius],
          },
          {
            label: {
              fontSize: labelFontSize,
            },
            radius: [outerRadius, innerRadius],
          },
        ],
      };

      this.myChart.setOption(adapterOption);

      // 收到调用resize才能产生效果
      this.myChart.resize();
    },

    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > 1) {
          this.currentPage = 0;
        }

        this.updateChart();
      }, 3000);
    },
  },
};
</script>
<style lang=""></style>
